package com.sduept.nwld.kpi.controller;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.core.dao.SuperDAO;
import com.sduept.generator.ObjectId;
import com.sduept.nwld.kpi.datasource.KpiDataSource;
import com.sduept.nwld.kpi.manager.KpiManager;
import com.sduept.nwld.kpi.model.KpiLevel;
import org.primefaces.event.CellEditEvent;
import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.ArrayList;
import java.util.List;

@Named
@ViewScoped
public class KpiDictController extends AbstractController {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8769647374639629490L;
	@Autowired
	private SuperDAO dao;
	@Autowired
	private KpiManager km;

	private List<KpiLevel> levels = new ArrayList<>();

	private List<KpiDataSource> ds = new ArrayList<>();

	private KpiLevel selectedLevel = null;
	private KpiDataSource selectedDs = null;

	@PostConstruct
	private void init() {
		levels = dao.findAll(KpiLevel.class, "order by sno");
		ds = dao.findAll(KpiDataSource.class);
	}

	public void createLevel() {
		KpiLevel kl = new KpiLevel();
		kl.setId(ObjectId.get().toString());
		kl.setSno(levels.size() + 1);
		levels.add(kl);
		dao.create(kl, KpiLevel.class);
	}

	public void deleteLevel() {
		if (selectedLevel != null) {
			try {
				dao.delete(selectedLevel.getId(), KpiLevel.class);
				levels.remove(selectedLevel);
				dao.nativeUpdate("update t_kpi_kpinormal set kpiLevel = null where kpiLevel = '" + selectedLevel.getId()
						+ "'; ");
				selectedLevel = null;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void createDs() {
		KpiDataSource bean = new KpiDataSource();
		bean.setName("");
		dao.create(bean, KpiDataSource.class);
		ds.add(0, bean);
	}

	public void deleteDs() {
		if (selectedDs != null) {
			try {
				km.deleteDatasourceById((selectedDs.getId()));
				ds.remove(selectedDs);
				selectedDs = null;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void onLevelRowSelect(SelectEvent event) {
		selectedLevel = (KpiLevel) event.getObject();
	}

	public void onLevelCellEdit(CellEditEvent event) {
		selectedLevel = null;
		for (KpiLevel k : levels) {
			if (k.getId().equals(event.getRowKey())) {
				selectedLevel = k;
				break;
			}
		}
		if (selectedLevel != null)
			dao.update(selectedLevel, KpiLevel.class);
	}

	public void onDsRowSelect(SelectEvent event) {
		selectedDs = (KpiDataSource) event.getObject();
	}

	public void onDsCellEdit(CellEditEvent event) {
		selectedDs = null;
		for (KpiDataSource k : ds) {
			if (k.getId().equals(event.getRowKey())) {
				selectedDs = k;
				break;
			}
		}
		if (selectedDs != null)
			dao.update(selectedDs, KpiDataSource.class);
	}

	public List<KpiLevel> getLevels() {
		return levels;
	}

	public void setLevels(List<KpiLevel> levels) {
		this.levels = levels;
	}

	public KpiLevel getSelectedLevel() {
		return selectedLevel;
	}

	public void setSelectedLevel(KpiLevel selectedLevel) {
		this.selectedLevel = selectedLevel;
	}

	public List<KpiDataSource> getDs() {
		return ds;
	}

	public void setDs(List<KpiDataSource> ds) {
		this.ds = ds;
	}

	public KpiDataSource getSelectedDs() {
		return selectedDs;
	}

	public void setSelectedDs(KpiDataSource selectedDs) {
		this.selectedDs = selectedDs;
	}

}
